package com.sttk.fastMark.camera.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.sttk.fastMark.camera.BaseAc;
import com.sttk.fastMark.camera.Content;
import com.sttk.fastMark.camera.R;
import com.sttk.fastMark.camera.basic.utils.SPUtil;
import com.sttk.fastMark.camera.databinding.ActivityCountBinding;

public class CountActivity extends BaseAc<ActivityCountBinding> implements View.OnClickListener {

    private long number;
    private boolean isAutoCount;

    @Override
    protected int onCreate() {
        return R.layout.activity_count;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCountBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivAdd.setOnClickListener(this);
        mDataBinding.ivMin.setOnClickListener(this);
        mDataBinding.ivAutoCount.setOnClickListener(this);
        mDataBinding.tvSave.setOnClickListener(this);

        mDataBinding.etCount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String str = charSequence.toString();
                if (TextUtils.isEmpty(str)) {
                    mDataBinding.ivCountClear.setVisibility(View.GONE);
                } else {
                    mDataBinding.ivCountClear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivAdd) {//加
            String input = mDataBinding.etCount.getText().toString();
            if (TextUtils.isEmpty(input)) return;
            long currentNumber = Long.parseLong(input);
            if (currentNumber < (1e16 - 1)) {
                currentNumber++;
            }
            mDataBinding.etCount.setText(currentNumber + "");
        } else if (id == R.id.ivMin) {//减
            String input = mDataBinding.etCount.getText().toString();
            if (TextUtils.isEmpty(input)) return;
            long currentNumber = Long.parseLong(input);
            if (currentNumber > 0) {
                currentNumber--;
            }
            mDataBinding.etCount.setText(currentNumber + "");
        } else if (id == R.id.ivAutoCount) {//自动计数
            isAutoCount = !isAutoCount;
            mDataBinding.ivAutoCount.setImageResource(isAutoCount ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivCountClear) {//清空
            mDataBinding.etCount.setText("");
        } else if (id == R.id.tvSave) {//保存
            String input = mDataBinding.etCount.getText().toString();
            if (TextUtils.isEmpty(input)) {
                ToastUtils.showShort(getString(R.string.please_input_count));
                return;
            }
            long currentNumber = Long.parseLong(input);
            EditCustomWatermarkActivity.watermarkBean.getCustomBean().setOpenCount(true);
            SPUtil.putLong(mContext, Content.count, currentNumber);
            SPUtil.putBoolean(mContext, Content.isAutoCount, isAutoCount);
            finish();
        }
    }

    @Override
    protected void initData() {
        number = SPUtil.getLong(mContext, Content.count, 1L);
        isAutoCount = SPUtil.getBoolean(mContext, Content.isAutoCount, true);
        mDataBinding.etCount.setText(number + "");
        mDataBinding.ivAutoCount.setImageResource(isAutoCount ? R.drawable.akai : R.drawable.aguan);
    }
}